package org.misty.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Misty on 2021-04-24
 */
public interface ListUtils {
    /**
     * 连续非空元素的个数
     */
    // TODO
    static <T> int sequentialCount(List<T> list, int from) {
        return sequentialCount(list, from, Integer.MAX_VALUE);
    }

    // TODO
    static <T> int sequentialCount(List<T> list, int from, int to) {
        if (from < 0) return 0;
        int size = list.size();
        to = Math.min(to, size);
        int loop = Math.abs(from - to);

        int count = 0;

        for (int i = 0; i < loop; i++) {

        }

        for (int i = from; i <= to; i += 1) {
            T item = list.get(i);
            if (item != null) {
                count++;
            } else {
                break;
            }
        }
        return count;
    }

    static <T> List<T> sequentialMove(List<T> list, int from, int step) {
        @SuppressWarnings("unchecked")
        T[] array = (T[]) list.toArray();

        int size = array.length;
        if (from < 0 || from >= size) return list;

        int loop = Math.abs(step);
        if (loop == 0) return list;

        for (int i = 0; i < loop; i++) {
            T prev = null;
            if (step > 0) {
                for (int k = from; k < size; k++) {
                    T temp = array[k];
                    array[k] = prev;
                    if (temp == null) break;
                    prev = temp;
                }
                from += 1;
            } else {
                for (int k = from; k >= 0; k--) {
                    T temp = array[k];
                    array[k] = prev;
                    if (temp == null) break;
                    prev = temp;
                }
                from -= 1;
            }
        }

        return new ArrayList<>(Arrays.asList(array));
    }
}
